package com.tyz.hellocst.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.tyz.hellocst.AppContext;
import com.tyz.hellocst.MainActivity;
import com.tyz.hellocst.R;
import com.tyz.hellocst.database.DBOperate;
import com.tyz.hellocst.entity.Goods;
import com.tyz.hellocst.entity.Person;

import java.util.ArrayList;

public class LoginActivity extends AppCompatActivity {

    private EditText etName, etPassWord;
    private Button btReg, btLogin,btn_Forget;
    private CheckBox cbPWDdVisibility;

    private DBOperate dbOperate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
       // setTitle(getString(R.string.title_activity_login));

        etName = findViewById(R.id.Username);
        etPassWord = findViewById(R.id.Password);

        btReg = findViewById(R.id.Sign_up);
        btLogin = findViewById(R.id.Sign_in);
        btn_Forget=findViewById(R.id.Forgetpassword);

        cbPWDdVisibility = (CheckBox) findViewById(R.id.login_checkBox);

        dbOperate = new DBOperate(this);

        //ceshitianjia();//测试用,初始化数据库

        initPermissions();//初始化权限
        initEditText();//初始化EditText

       cbPWDdVisibility.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //如果选中，显示密码
                    etPassWord.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //否则隐藏密码
                    etPassWord.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        //        注册
        btReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userRegister();
            }
        });//setOnClickListener

        //        登录
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userLogin();
            }
        });
        btn_Forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userforgetpassword();

            }
        });


    }//onCreate

    private void initPermissions() {
        ArrayList permissions = new ArrayList();
        permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        permissions.add(Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);

        //请求权限方法
        ActivityCompat.requestPermissions(LoginActivity.this,
                ((String[]) permissions.toArray(new String[permissions.size()])), 2);
    }

    private void initEditText() {
        Intent intent = getIntent();
        String uName = intent.getStringExtra("userName");
        String uPassWord = intent.getStringExtra("userPassword");
        if (uName == null && uPassWord == null) {
//            etName.setText("root");
//            etPassWord.setText("password");
            etName.setSelection(etName.getText().length());     //把光标设置到当前文本末尾
        } else {
            etName.setText(uName);
            etPassWord.setText(uPassWord);
        }
    }



    private void userRegister() {
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), RegisterActivity.class);
        startActivity(intent);
    }

    private void userLogin() {
        String userName = etName.getText().toString();
        String userPassWord = etPassWord.getText().toString();
        if (userName.isEmpty()) {
            etName.requestFocus();
            etName.setError(getString(R.string.a_login_userNameEmptyError));
            return;
        }
        if (userPassWord.isEmpty()) {
            etPassWord.requestFocus();
            etPassWord.setError(getString(R.string.a_login_userPwdEmptyError));
            return;
        }

        dbOperate.openDB();
        boolean flag0 = dbOperate.findPerson(userName, userPassWord);
        boolean flag1 = dbOperate.havePerson(userName);
        Person person = dbOperate.getPerson(userName);
        dbOperate.closeDb();

        if (flag0) {//登录成功
            AppContext.setUserName(person.getUserName());
            AppContext.setName(person.getName());
            AppContext.setUserMd5(person.getUserMd5());

            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            if (flag1) {//账户存在,登录失败
                etPassWord.requestFocus();
                etPassWord.setError(getString(R.string.a_login_userPwdError));
                Toast.makeText(getApplicationContext(), "密码错误！", Toast.LENGTH_SHORT).show();
            } else {
                etName.requestFocus();
                etName.setError(getString(R.string.a_login_noUserNameError));
                Toast.makeText(getApplicationContext(), "账户不存在！", Toast.LENGTH_SHORT).show();
            }//if-else
        }//if-else
    }
    private void userforgetpassword(){

            Intent intent = new Intent(LoginActivity.this, ForgetpasswordActivity.class);
            startActivity(intent);


    }
}
